﻿using Bussiness.DEV;
using Model;
using Syntec.Remote;
using System.Collections.Generic;

namespace Bussiness
{
    public class SYNTEC : IDEV
    {
        public Device Device { get; set; }
        public List<DevicePOI> DevicePOIList { get; set; }
        SyntecRemoteCNC SyntecRemoteCNC = null;

        public Result Connect()
        {
            Result result = new Result();
            SyntecRemoteCNC = new SyntecRemoteCNC(Device.IP);
            result.Success = true;
            return result;
        }

        public Result<List<Cfg_Field>> Read()
        {
            Result<List<Cfg_Field>> result = new Result<List<Cfg_Field>>();
            foreach (var devicePOI in DevicePOIList)
            {
                Cfg_Field cfg_Field = new Cfg_Field();
                cfg_Field.name = devicePOI.DevPOIAlias;
                short axes, maxAxes;
                string cncType, series, nc_Ver;
                string[] axisName;
                short cncResult = SyntecRemoteCNC.READ_information(out axes, out cncType, out maxAxes, out series, out nc_Ver, out axisName);
                if(cncResult == (short)SyntecRemoteCNC.ErrorCode.NormalTermination)
                {
                    result.Success = true;
                    switch (devicePOI.DevPOIAlias)
                    {
                        case "可控制轴数":
                            cfg_Field.value = axes.ToString();
                            break;
                        case "CNC类型":
                            cfg_Field.value = cncType;
                            break;
                        case "最大轴数":
                            cfg_Field.value = maxAxes.ToString();
                            break;
                        case "series":
                            cfg_Field.value = series;
                            break;
                        case "NC版本":
                            cfg_Field.value = nc_Ver;
                            break;
                        case "各轴坐标名称":
                            if (axisName != null)
                            {
                                string _axisname = string.Empty;
                                foreach (var str in axisName)
                                {
                                    if (!string.IsNullOrEmpty(_axisname))
                                    {
                                        _axisname += ",";
                                    }

                                    _axisname += str;
                                }

                                cfg_Field.value = _axisname;
                            }
                            break;
                    }
                }
                

                int curSeq;
                string mainProg, curProg, mode, status, alarm, emg;
                cncResult = SyntecRemoteCNC.READ_status(out mainProg, out curProg, out curSeq, out mode, out status, out alarm, out emg);
                if(cncResult == (short)SyntecRemoteCNC.ErrorCode.NormalTermination)
                {
                    result.Success = true;
                    switch (devicePOI.DevPOIAlias)
                    {
                        case "主程序名":
                            cfg_Field.value = mainProg;
                            break;
                        case "当前程序名":
                            cfg_Field.value = curProg;
                            break;
                        case "当前工序号":
                            cfg_Field.value = curSeq.ToString();
                            break;
                        case "工作模式":
                            cfg_Field.value = mode;
                            break;
                        case "设备状态":
                            cfg_Field.value = status + "," + "," + alarm + "," + emg;
                            break;
                    }
                }

                short decPoint;
                string[] axisName2, unit;
                float[] mach, abs, rel, dist;
                cncResult = SyntecRemoteCNC.READ_position(out axisName2, out decPoint, out unit, out mach, out abs, out rel, out dist);
                if(cncResult == (short)SyntecRemoteCNC.ErrorCode.NormalTermination)
                {
                    result.Success = true;
                    switch (devicePOI.DevPOIAlias)
                    {
                        case "各轴坐标单位":
                            if (unit != null)
                            {
                                string _unit = string.Empty;
                                foreach (var str in unit)
                                {
                                    if (!string.IsNullOrEmpty(_unit))
                                    {
                                        _unit += ",";
                                    }

                                    _unit += str;
                                }

                                cfg_Field.value = _unit;
                            }
                            break;
                        case "小数位数":
                            cfg_Field.value = decPoint.ToString();
                            break;
                        case "各轴机械坐标":
                            if (mach != null)
                            {
                                string _mach = string.Empty;
                                foreach (var str in mach)
                                {
                                    if (!string.IsNullOrEmpty(_mach))
                                    {
                                        _mach += ",";
                                    }

                                    _mach += str;
                                }

                                cfg_Field.value = _mach;
                            }
                            break;
                        case "各轴绝对坐标":
                            if (abs != null)
                            {
                                string _abs = string.Empty;
                                foreach (var str in abs)
                                {
                                    if (!string.IsNullOrEmpty(_abs))
                                    {
                                        _abs += ",";
                                    }

                                    _abs += str;
                                }

                                cfg_Field.value = _abs;
                            }
                            break;
                        case "各轴相对坐标":
                            if (rel != null)
                            {
                                string _rel = string.Empty;
                                foreach (var str in rel)
                                {
                                    if (!string.IsNullOrEmpty(_rel))
                                    {
                                        _rel += ",";
                                    }

                                    _rel += str;
                                }

                                cfg_Field.value = _rel;
                            }
                            break;
                        case "各轴剩余距离":
                            if (dist != null)
                            {
                                string _dist = string.Empty;
                                foreach (var str in dist)
                                {
                                    if (!string.IsNullOrEmpty(_dist))
                                    {
                                        _dist += ",";
                                    }

                                    _dist += str;
                                }

                                cfg_Field.value = _dist;
                            }
                            break;
                    }
                }

                float ovFeed, ovSpindle, actFeed;
                int actSpindle;
                cncResult = SyntecRemoteCNC.READ_spindle(out ovFeed, out ovSpindle, out actFeed, out actSpindle);
                if (cncResult == (short)SyntecRemoteCNC.ErrorCode.NormalTermination)
                {
                    result.Success = true;
                    switch (devicePOI.DevPOIAlias)
                    {
                        case "进给率":
                            cfg_Field.value = ovFeed.ToString();
                            break;
                        case "主轴转速":
                            cfg_Field.value = ovSpindle.ToString();
                            break;
                        case "实际进养率":
                            cfg_Field.value = actFeed.ToString();
                            break;
                        case "实际主轴转速":
                            cfg_Field.value = actSpindle.ToString();
                            break;
                    }
                }

                int power, cutting, cycle, workTime;
                cncResult = SyntecRemoteCNC.READ_time(out power, out cutting, out cycle, out workTime);
                if (cncResult == (short)SyntecRemoteCNC.ErrorCode.NormalTermination)
                {
                    result.Success = true;
                    switch (devicePOI.DevPOIAlias)
                    {
                        case "开机时间":
                            cfg_Field.value = power.ToString();
                            break;
                        case "切削时间":
                            cfg_Field.value = cutting.ToString();
                            break;
                        case "循环时间":
                            cfg_Field.value = cycle.ToString();
                            break;
                        case "加工时间":
                            cfg_Field.value = workTime.ToString();
                            break;
                    }
                }

                int part_count;
                int require_part_count;
                int total_part_count;
                cncResult = SyntecRemoteCNC.READ_part_count(out part_count, out require_part_count, out total_part_count);
                if (cncResult == (short)SyntecRemoteCNC.ErrorCode.NormalTermination)
                {
                    result.Success = true;
                    switch (devicePOI.DevPOIAlias)
                    {
                        case "工件数":
                            cfg_Field.value = part_count.ToString();
                            break;
                        case "需求工件数":
                            cfg_Field.value = require_part_count.ToString();
                            break;
                        case "总工件数":
                            cfg_Field.value = total_part_count.ToString();
                            break;
                    }
                }

                string[] title;
                cncResult = SyntecRemoteCNC.READ_offset_title(out title);
                if (cncResult == (short)SyntecRemoteCNC.ErrorCode.NormalTermination)
                {
                    result.Success = true;
                    if (title != null)
                    {
                        string _title = string.Empty;
                        foreach (var str in title)
                        {
                            if (!string.IsNullOrEmpty(_title))
                            {
                                _title += ",";
                            }

                            _title += str;
                        }

                        cfg_Field.value = _title;
                    }
                }

                double[][] offset;
                cncResult = SyntecRemoteCNC.READ_offset_all(out offset);
                if (cncResult == (short)SyntecRemoteCNC.ErrorCode.NormalTermination)
                {
                    result.Success = true;
                    if (offset != null)
                    {
                        string _offset = string.Empty;
                        for (int i = 0; i < offset.Length; i++)
                        {
                            string tmp = string.Empty;
                            for (int k = 0; k < offset[i].Length; k++)
                            {
                                if (!string.IsNullOrEmpty(tmp))
                                {
                                    tmp += " ";
                                }

                                tmp += offset[i][k].ToString();
                            }

                            if (!string.IsNullOrEmpty(_offset))
                            {
                                _offset += ",";
                            }

                            _offset += tmp;
                        }

                        cfg_Field.value = _offset;
                    }
                }
            }
            
            return result;
        }
    }
}
